home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / abbrev5.zip / ABBREV.M < prev    next >
Text File  |  1990-01-22  |  15KB  |  380 lines

  1. ;**************************************************************************
  2. ;*                                                                                                  *
  3. ;*     If you are a previous user of ABBREV (previous to ABBREV4),              *
  4. ;*     you will need to replace the autoload's that were provided              *
  5. ;*     with that version    with the ones specified below.                          *
  6. ;*                                                                                               *
  7. ;*     ABBREV5 allows abbrev to work in conjunction with Brief's              *
  8. ;*     file name completion facility (i.e. if <Tab> is pressed                  *
  9. ;*     while an abbrevion is on the command line, the command                  *
  10. ;*     will be expanded by abbrev, and then handed over to Brief's              *
  11. ;*     file completion facility).  For user's of ABBREV4, simply              *
  12. ;*     compile this file and the new ablookup.cb, and the new facility      *
  13. ;*    will be in place.                                                                      *
  14. ;*                                                                                               *
  15. ;*     For users of previous versions, follow the instructions                  *
  16. ;*     below.                                                                                  *
  17. ;*                                                                                                  *
  18. ;**************************************************************************
  19.  
  20. ;**************************************************************************
  21. ;*                                                                                                  *
  22. ;*      ABBREV.CB    (Version 5)                                                              *
  23. ;*                                                                                               *
  24. ;*      This macro provides a replacement for edit_file and read_file.  It *
  25. ;*      allows arbitrary abbreviation of drive and path combinations in      *
  26. ;*      file names (and may even be used to abbreviate full file names).      *
  27. ;*                                                                                               *
  28. ;*      The if there is a slash or backslash in the path (after column 1), *
  29. ;*      then abbrev looks up a replacement for what preceeds the              *
  30. ;*      1st slash (backslash) and passes it on to the next                      *
  31. ;*      program in the chain.     If there is no slash or backslash,              *
  32. ;*      abbrev checks for a replacement of the entire name                      *
  33. ;*      specified.  (This is for using an abbreviation for a                      *
  34. ;*      file name.)  It is not necessary to use the last                          *
  35. ;*      directory name as the abbreviation, and you may                          *
  36. ;*      supply several abbreviations for a single directory.                      *
  37. ;*                                                                                               *
  38. ;*      Finally, if you wish to pass a name through without being              *
  39. ;*      altered by abbrev, then precede it with an exclaimation point.      *
  40. ;*                                                                                               *
  41. ;*      If "foo" was the abbreviation for "c:\new\dos\foo"                      *
  42. ;*                                                                                               *
  43. ;*      then (edit_file "foo\filename") would pass on:                              *
  44. ;*                                                                                               *
  45. ;*              (edit_file "c:\new\dos\foo\filename").                              *
  46. ;*                                                                                               *
  47. ;*      The abbreviations are kept in a file called "abbrev.dat".              *
  48. ;*      This file goes in the last directory specified by your                  *
  49. ;*      BPATH environment variable.    If you would like it in a                  *
  50. ;*      different directory, then you may specify this with the                  *
  51. ;*      BABBREV environment variable.  The macro "abbrev" (invoked with      *
  52. ;*      F10) will bring the abbrev.dat file into a Brief buffer to be          *
  53. ;*      edited.                                                                                  *
  54. ;*                                                                                               *
  55. ;*      Each abbreviation is specified on 1 line of the file.                  *
  56. ;*      The line is of the form:                                                          *
  57. ;*                                                                                               *
  58. ;*      <abbreviation> <whitespace> <substitution>                                  *
  59. ;*                                                                                               *
  60. ;*      The <abbreviation> must begin in column 1.    The line                      *
  61. ;*      for the above example would be:                                                  *
  62. ;*                                                                                               *
  63. ;*      foo         c:\new\dos\foo                                                          *
  64. ;*                                                                                               *
  65. ;*      To inspire you of its usefulness, part of my current file is          *
  66. ;*      setup as follows:                                                                  *
  67. ;*                                                                                               *
  68. ;*      autoexec c:\autoexec.bat                                                          *
  69. ;*      exec      c:\autoexec.bat                                                          *
  70. ;*      config     c:\config.sys                                                              *
  71. ;*      ap         d:\pinball\apple                                                          *
  72. ;*      apple     d:\pinball\apple                                                          *
  73. ;*      util      d:\util                                                                      *
  74. ;*      u          d:\util                                                                      *
  75. ;*      uni         d:\util\uni                                                              *
  76. ;*      mine      c:\soft\brief\macros\mine                                              *
  77. ;*      m          c:\soft\brief\macros\mine                                              *
  78. ;*      macros     c:\soft\brief\macros                                                  *
  79. ;*      mac         c:\soft\brief\macros                                                  *
  80. ;*      macs      c:\soft\brief\macros                                                  *
  81. ;*      inc         c:\soft\c\include                                                      *
  82. ;*      include  c:\soft\c\include                                                      *
  83. ;*                                                                                               *
  84. ;*      To install, add the following to your initials macro                      *
  85. ;*      (after any other replacements to edit_file or read_file):              *
  86. ;*                                                                                               *
  87. ;*        (autoload "abbrev.cm" "edit_file" "read_file" "abbrev")                    *
  88. ;*      (autoload "ablookup.cm" "ab_lookup","get_abbrev_file")                    *
  89. ;*                                                                                               *
  90. ;*      Place "abbrev.cm" and "ablookup.cm" in a BPATH directory              *
  91. ;*      (e.g. c:\brief\macros).                                                          *
  92. ;*                                                                                               *
  93. ;*      Abbrev keeps the abbreviation file in a system buffer.                  *
  94. ;*      The 1st call will create the system buffer.   Subsequent calls      *
  95. ;*      will run faster.                                                                      *
  96. ;*                                                                                               *
  97. ;*      Larry DeMar     July, 1989                                                          *
  98. ;*                                                                                               *
  99. ;*      Updated for FILE_ED3 compatability,  August, 1989.   -Led              *
  100. ;*                                                                                               *
  101. ;*      Updated to allow suffix.cb to use the utilities without loading      *
  102. ;*      replacement macros (ABBREV4) October, 1989  -Led.                          *
  103. ;*                                                                                                  *
  104. ;**************************************************************************
  105. ;*                                                                                                  *
  106. ;*      Version 5 has the following improvements:                                  *
  107. ;*                                                                                               *
  108. ;*      Updated to work with Brief's File completion facility.                  *
  109. ;*                                                                                               *
  110. ;*      Supresses command line jibberish generated by                              *
  111. ;*         called primitives.                                                              *
  112. ;*                                                                                               *
  113. ;*      Automatically Invokes Brief's file completion if * or ?                  *
  114. ;*         is on command    line when <Enter> is pressed.  Please note          *
  115. ;*          that the time "Expanding Wildcard..." is displayed,                  *
  116. ;*          Brief is doing its file completion.  Abbrev does very              *
  117. ;*          little during this delay (compare to the time required              *
  118. ;*          without abbrev installed).                                                  *
  119. ;*                                                                                               *
  120. ;*      January, 1990 -Led                                                                  *
  121. ;*                                                                                                  *
  122. ;**************************************************************************
  123. #define ABBREV_FILE_NAME "abbrev.dat"
  124.  
  125. (extern ab_lookup
  126.          get_abbrev_file
  127.          _bad_key)
  128.  
  129. (replacement edit_file
  130.    (
  131.       (string e_parm)
  132.       (if (!= (inq_called) "")
  133.          (return (edit_file))
  134.       )
  135.       (if (! (get_parm 0 e_parm "File: "))
  136.          (return)
  137.       )
  138.  
  139.       ;        Before passing our value down...see if there are any wildcard
  140.       ;        characters.
  141.  
  142.       (if (== (= e_parm (wild e_parm "File: ")) "")
  143.          (return)
  144.       )
  145.       (edit_file (abbr e_parm))
  146.    )
  147. )
  148. (replacement read_file
  149.    (
  150.       (string r_parm)
  151.       (if (!= (inq_called) "")
  152.          (return (read_file))
  153.       )
  154.       (if (! (get_parm 0 r_parm "File to read: "))
  155.          (return)
  156.       )
  157.       (if (== (= r_parm (wild r_parm "File to read: ")) "")
  158.          (return)
  159.       )
  160.       (read_file (abbr r_parm))
  161.    )
  162. )
  163. ;**************************************************************************
  164. ;*                                                                                                  *
  165. ;*     WILD                                                                                      *
  166. ;*                                                                                               *
  167. ;*     This is called to check for wildcard characters in the                  *
  168. ;*     prompt and force file name completion if wildcard                          *
  169. ;*     characters are detected.                                                          *
  170. ;*                                                                                               *
  171. ;*     It returns the final prompt result, or "" if the                          *
  172. ;*     user escapes.                                                                          *
  173. ;*                                                                                                  *
  174. ;**************************************************************************
  175. (macro wild
  176.    (
  177.       (string e_parm
  178.               our_prompt
  179.       )
  180.       (get_parm 0 e_parm)
  181.       (get_parm 1 our_prompt)
  182.       (while (|| (index e_parm "*") (index e_parm "?"))
  183.          (
  184.  
  185.             ;        Put this public service message up.
  186.             ;        Brief takes tremendous amounts of time expanding
  187.             ;        Wildcards.
  188.  
  189.             (message "Expanding wildcard...")
  190.             (push_back (key_to_int "<Tab>"))   ; Put a tab out to tell Brief to expand
  191.             (if (! (get_parm NULL e_parm our_prompt NULL e_parm))
  192.                (return "")
  193.             )
  194.          )
  195.       )
  196.       (return e_parm)
  197.    )
  198. )
  199. ;**************************************************************************
  200. ;*                                                                                                  *
  201. ;*      ABBR                                                                                      *
  202. ;*                                                                                               *
  203. ;*      This is called to expand an abbreviation if necessary.                  *
  204. ;*                                                                                               *
  205. ;*      It returns the string that should be passed on.                          *
  206. ;*                                                                                                  *
  207. ;**************************************************************************
  208. (macro abbr
  209.    (
  210.       (int slash_index)
  211.       (int ab_buf)
  212.       (global ab_buf)
  213.       (string abbreviation
  214.               file_name
  215.               first_char   ; allow leading slash or backslash as a courtesy
  216.       )
  217.       (get_parm 0 file_name)
  218.       (if (== (substr file_name 1 1) "!")   ; check for bang
  219.          (return (substr file_name 2))   ; strip it and we're done.
  220.       )
  221.       ; 
  222.       ;   As a courtesy, if the user preceeded his abbreviation
  223.       ;   with slash or backslash (from using dos too much)
  224.       ;   lookup what follows the leading seperator
  225.       ; 
  226.       ;   If you want to use that leading slash or backslash
  227.       ;   to shut off abbrev, then comment out the next 2
  228.       ;   Brief statements (next 6 lines).
  229.       ; 
  230.       (= first_char (substr file_name 1 1))    ; get 1st char
  231.       (if (|| (== first_char "/") (== first_char "\\"))
  232.          (= file_name (substr file_name 2))   ; lookup rest
  233.       ;else
  234.          (= first_char "")   ; nothing stripped
  235.       )
  236.       ; 
  237.       ;   now we have to see if there is a path separator.
  238.       ; 
  239.       (= slash_index (get_first_seperator file_name))
  240.       ;       slash_index is now zero if there are now separators
  241.       ;       and has the position of the 1st seperator if non-zero.
  242.       ; 
  243.       (if slash_index
  244.          (= abbreviation (substr file_name 1 (- slash_index 1)))
  245.       ;else
  246.          (= abbreviation file_name)
  247.       )
  248.       (if (|| (index abbreviation "*") (index abbreviation "?"))   ; for FILE_ED3 if it has a star
  249.          (= abbreviation (+ first_char abbreviation))   ; don't look up!
  250.       ;else
  251.          (if (! (ab_lookup ABBREV_FILE_NAME abbreviation ab_buf "abbrev.$b$"))
  252.             (= abbreviation (+ first_char abbreviation))   ; not found..put 1st char back.
  253.          )
  254.          ; 
  255.          ;   now....if there was a path...we must concatonate whats beyond 1st seperator
  256.          ; 
  257.       )
  258.       (if slash_index
  259.          (= abbreviation (+ abbreviation (substr file_name slash_index)))
  260.       )
  261.       (return abbreviation)
  262.    )
  263. )
  264. ;**************************************************************************
  265. ;*                                                                                                  *
  266. ;*      ABBREV                                                                                  *
  267. ;*                                                                                               *
  268. ;*      This is called to bring the abbreviation file into the                  *
  269. ;*      editor.  It first deletes the system buffer such that                  *
  270. ;*      changes (that are written) will be used on the next                      *
  271. ;*      request.                                                                              *
  272. ;*                                                                                                  *
  273. ;**************************************************************************
  274. (macro abbrev
  275.    (
  276.       (if ab_buf
  277.          (delete_buffer ab_buf)
  278.       )
  279.       (= ab_buf 0)
  280.       (edit_file (get_abbrev_file "abbrev.dat"))
  281.    )
  282. )
  283. ;**************************************************************************
  284. ;*                                                                                                  *
  285. ;*      GET_FIRST_SEPERATOR                                                                  *
  286. ;*                                                                                               *
  287. ;*      This is called to return the position of the 1st seperator              *
  288. ;*      (slash or backslash) in a file name.    It returns 0 if                  *
  289. ;*      no seperators exist, or the index of the 1st one it                      *
  290. ;*      finds.                                                                                  *
  291. ;*                                                                                                  *
  292. ;**************************************************************************
  293. (macro get_first_seperator
  294.    (
  295.       (int slash_index
  296.            bslash_index
  297.       )
  298.       (string file_name)
  299.       (get_parm 0 file_name)
  300.       (= slash_index (index file_name "/"))
  301.       (= bslash_index (index file_name "\\"))
  302.       (if (&& slash_index bslash_index)   ; if both seperators are there
  303.          (if (< bslash_index slash_index)
  304.             (= slash_index bslash_index)   ; then take the lower
  305.          ;else
  306.          )
  307.       ;else
  308.          (if bslash_index   ; is 1st zero?
  309.             (= slash_index bslash_index)   ; yep...use 2nd.
  310.          )
  311.       )
  312.       (return slash_index)
  313.    )
  314. )
  315. ;**************************************************************************
  316. ;*                                                                                                  *
  317. ;*     _BAD_KEY                                                                                  *
  318. ;*                                                                                               *
  319. ;*     This replacement for bad key allows Abbrev and Brief's                  *
  320. ;*     file name completion to happily co-exist.                                      *
  321. ;*                                                                                               *
  322. ;*     If the current command is edit_file or read_file, and                      *
  323. ;*     the bad key is <Tab>, then we see if there is a substitution          *
  324. ;*     to make on the command line.  If there is, we make the                  *
  325. ;*     substitution, and push a <Tab> back on the keyboard stack.              *
  326. ;*                                                                                               *
  327. ;*     The <Tab> is necessary because Brief's _bad_key file completion      *
  328. ;*     routine reads directly from the command line (as opposed to              *
  329. ;*     passing the command string up and down the chain).  We then              *
  330. ;*     ignore the 2nd <Tab> and let the file completion facility              *
  331. ;*     do its thing.                                                                          *
  332. ;*                                                                                                  *
  333. ;**************************************************************************
  334. (replacement _bad_key
  335.    (
  336.       (int ab_flag)
  337.       (global ab_flag)
  338.       (int key_pressed)
  339.       (= key_pressed (read_char))
  340.       (string cmd_lin
  341.               command
  342.       )
  343.       (= command (inq_command))
  344.  
  345.       ;     No matter what key it was we put it back.
  346.       ;        If its a tab, it also goes back because we need to
  347.       ;        do a file name completion.
  348.  
  349.       (push_back key_pressed)
  350.       (if (&& (|| (== command "edit_file"
  351.          ) (== command "read_file")) (== key_pressed (key_to_int "<Tab>")))
  352.          (
  353.  
  354.             ;        If our flag is set, we have already processed.
  355.             ;        Clear the flag and let it through
  356.  
  357.             (if ab_flag
  358.                (
  359.                   (= ab_flag 0)
  360.                   (return (_bad_key))
  361.                )
  362.             )
  363.             (= cmd_lin (abbr (inq_cmd_line)))   ; abbreviate what's there
  364.             (if (== cmd_lin (inq_cmd_line))   ; if it didn't change
  365.                (return (_bad_key))   ;     we're done...pass it down.
  366.             )
  367.  
  368.             ;        We made a change.....we have to get it back to the command line
  369.  
  370.             (= ab_flag 1)   ; Cause us to ignore this req.
  371.             (return cmd_lin)   ; Pass back completed string.
  372.          )
  373.       )
  374.  
  375.       ;        Not of interest to us....pass the bad key down.
  376.  
  377.       (return (_bad_key))
  378.    )
  379. )
  380.